﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;

using UnityEngine;
using UnityEditor;
using DashFire.Assetbundle;

//namespace DashFire.Assetbundle
//{
  internal class ExportRes_All
  {
    internal static string s_ResBuildDiffFilePath = "ResBuildDiff_ab.txt";

    #region Menu Item 
    [MenuItem("Export/Export All/Export Client Resources", false, 11)]
    internal static void ExportClientResources()
    {
      if (!ExportConfig.Load()) {
        ExportConfig.Log("ExportConfig.Load failed!");
        return;
      }

      if (!ExportRes_Deployer.CleanOutputResDir()) {
        ExportConfig.Log("ExportRes_Deployer.CleanOutputResDir failed!");
        return;
      }

      if (!ExportRes_Sheet.ExportClientResources()) {
        ExportConfig.Log("ExportRes_Sheet.ExportClientResources failed!");
        return;
      }

      if (!ExportRes_UI.ExportClientResources()) {
        ExportConfig.Log("ExportRes_UI.ExportClientResources failed!");
        return;
      }

      if (!ExportRes_VersionInfo.ExportAll()) {
        ExportConfig.Log("ExportRes_VersionInfo.Export failed!");
        return;
      }

      if (!ExportRes_Deployer.CommitBuildInResources()) {
        ExportConfig.Log("ExportRes_Deployer.CommitBuildInResources failed!");
        return;
      }

      ExportConfig.Log("ExportRes_All.ExportClientResources Success!");
      UnityEngine.Debug.Log("ExportRes_All.ExportClientResources Success");
    }
    [MenuItem("Export/Export All/Export Diff Resources", false, 12)]
    internal static void ExportDiffResources()
    {
      if (!ExportConfig.Load()) {
        ExportConfig.Log("ExportConfig.Load failed!");
        return;
      }

      if (!LoadDiffFile()) {
        ExportConfig.Log("LoadDiffFile failed!");
        return;
      }

      if (!ExportRes_Deployer.CleanOutputResDir()) {
        ExportConfig.Log("ExportRes_Deployer.CleanOutputResDir failed!");
        return;
      }

      if (!ExportRes_Sheet.ExportDiffResources()) {
        ExportConfig.Log("ExportRes_Sheet.ExportClientResources failed!");
        return;
      }

      if (!ExportRes_UI.ExportDiffResources()) {
        ExportConfig.Log("ExportRes_UI.ExportClientResources failed!");
        return;
      }

      if (!ExportRes_SoloRes.ExportDiffResources()) {
        ExportConfig.Log("ExportRes_UI.ExportClientResources failed!");
        return;
      }

      if (!ExportRes_VersionInfo.ExportServer()) {
        ExportConfig.Log("ExportRes_VersionInfo.Export failed!");
        return;
      }

      ExportConfig.Log("ExportRes_All.ExportDiffResources Success");
      UnityEngine.Debug.Log("ExportRes_All.ExportDiffResources Success");
    }
    #endregion
    internal static bool LoadDiffFile()
    {
      ExportConfig.Log("--------------------------------------------------------------");
      ExportConfig.Log("LoadDiffFile Start...");

      string diffPath = string.Format("{0}/{1}", ExportConfig.GetGroupPath(), s_ResBuildDiffFilePath);
      if (!File.Exists(diffPath)) {
        ExportConfig.Log("ResBuildGenerator.LoadDiffFile file miss. diffPath:" + diffPath);
        return false;
      }
      string[] contentList = File.ReadAllLines(diffPath, System.Text.Encoding.UTF8);
      string tmpGuid = string.Empty;
      string tmpItemPath = string.Empty;
      if (contentList != null && contentList.Length > 0) {
        tmpGuid = string.Empty;
        tmpItemPath = string.Empty;
        foreach (string itemPath in contentList) {
          if (string.IsNullOrEmpty(itemPath)) {
            continue;
          }
          string[] content = itemPath.Split(new string[] { " " }, StringSplitOptions.RemoveEmptyEntries);
          if (content != null && content.Length > 0) {
            string contentFile = content[content.Length - 1];
            contentFile = ExportConfig.ConvertPathSlash(contentFile);
            string contentFileLower = contentFile.ToLower();
            if (!contentFileLower.EndsWith(".cs")
              && !contentFileLower.EndsWith(".js")
              && !contentFileLower.EndsWith(".dll")
              && !contentFileLower.EndsWith(".a")
              && !contentFileLower.EndsWith(".meta")
              && (contentFileLower.StartsWith("assets/") 
                || contentFileLower.StartsWith("client/")
                || contentFileLower.StartsWith("public/"))
              && !contentFileLower.StartsWith("assets/editor")
              && !contentFileLower.StartsWith("assets/android")
              && !contentFileLower.StartsWith("assets/ios")
              && !contentFileLower.StartsWith("assets/plugin")
              && !contentFileLower.StartsWith("assets/scripts")
              && !contentFileLower.StartsWith("assets/xoptimzier")
              && !contentFileLower.StartsWith("assets/assetbundle")
              && !contentFileLower.StartsWith("assets/ngui")
              ) {
                ExportConfig.s_DiffContent.Add(contentFile);
            }
          }
        }
      }
      return true;
    }
    //[MenuItem("Export/Test")]
    internal static void Test()
    {
      //UnityEngine.Debug.Log(AssetDatabase.GUIDToAssetPath("49aae01bb87f65b43b03dca327e5b237"));
      //string assetPath = "assets/ui/resources/test/resources/ui/selectcreatehero.prefab";
      //string assetPath = "assets/ui/atlasesbag2/player/player01.unity";
      //UnityEngine.Debug.Log(ExportConfig.FormatAssetShortName(assetPath));
      //string content = "中国房地产市场目前正";
      string content = "abcdeabcde";
      UnityEngine.Debug.Log(content.Length);
    }
  }
//}
